package com.liang.maogouyuyin.base.lzy

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.res.ResourcesCompat
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.ImmersionBar
import com.liang.maogouyuyin.R
import com.liang.maogouyuyin.utils.lzy.ScreenUtils
import com.liang.maogouyuyin.widget.popup.MainPopup
import com.liang.publics.util.SharedPreferencesUtil
import com.lxj.xpopup.XPopup

abstract class LZYBaseActivity<VB : ViewBinding> : AppCompatActivity() {

    private lateinit var binding: VB
    private var toolbar: Toolbar?=null

    companion object {
        inline fun <reified T : LZYBaseActivity<*>> forward(context: Context, bundle: Bundle? = null) {
            val intent = Intent(context, T::class.java)
            if (bundle != null) {
                intent.putExtras(bundle)
            }
            context.startActivity(intent)
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = createBinding()
        setContentView(binding.root)
        setImmersionMode(true)
        toolbar=provideToolbar()
        if (toolbar!=null){
            initToolBar()
        }

        if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)) {
            if (!SharedPreferencesUtil.getStatus("MainPopup")) {
                XPopup.Builder(this)
                    .dismissOnTouchOutside(false)
                    .asCustom(MainPopup(this))
                    .show()
            }
        }

        initViews()
    }

    // 创建 ViewBinding 实例
    abstract fun createBinding(): VB

    // 提供子类的Toolbar引用
    // 抽象方法，子类需要提供 Toolbar 的 引用 给基类，如果没有 Toolbar 返回 null
    open fun provideToolbar(): Toolbar?{
        return null
    }

    // 初始化视图
    abstract fun initViews()

    private fun initToolBar(){
        setSupportActionBar(toolbar)
        supportActionBar?.title=null
    }

    @SuppressLint("RtlHardcoded")
    protected fun Toolbar.addToolBarBack(imgId:Int= R.mipmap.frame_toolbar_back, onclick:() -> Unit = {finish()}){
        val imageView = ImageView(this.context)
        imageView.id = R.id.frame_toolbar_back
        val layoutParams = Toolbar.LayoutParams(
            ScreenUtils.dip2px( 20,this.context),  ScreenUtils.dip2px( 20,this.context)
        )
        imageView.layoutParams = layoutParams
        layoutParams.gravity = android.view.Gravity.LEFT
        layoutParams.rightMargin = resources.getDimensionPixelSize(R.dimen.dp_10)
        toolbar?.addView(imageView, layoutParams)
        imageView.setImageResource(imgId)
        imageView.setOnClickListener {
            onclick()
        }
    }

    protected fun Toolbar.addToolBarTitle(titleString: String){
        val textView = TextView(this.context)
        textView.id = R.id.frame_toolbar_title
        val layoutParams = Toolbar.LayoutParams(
            Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT
        )
        textView.layoutParams = layoutParams
        textView.textSize = 18f
        textView.setTextColor(ResourcesCompat.getColor(resources,R.color.black,null))
        layoutParams.gravity = android.view.Gravity.CENTER
        toolbar?.addView(textView, layoutParams)
        textView.text=titleString
    }

    protected fun setImmersionMode(immersion:Boolean){
        if (immersion){
            ImmersionBar.with(this)
                .transparentStatusBar()
                .init()
        }
    }

    // 获取当前 Activity 的宽度
    protected fun getActivityWidth(): Int {
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        return displayMetrics.widthPixels
    }

    // 获取当前 Activity 的高度
    protected fun getActivityHeight(): Int {
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        return displayMetrics.heightPixels
    }
}
